package com.example.shop.controller;

import com.example.shop.demo.LoginRequest;
import com.example.shop.demo.LoginResponse;
import com.example.shop.entity.LoginInfo;
import com.example.shop.entity.User;
import com.example.shop.service.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author 吴
 * @Date 2025/3/26 10:11
 */
@Controller
@RequestMapping("/user")
@Tag(name = "UserController", description = "对User操作的接口")
public class UserController {

    @Autowired
    private UserService userService;

    // 查询单个用户
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Integer userid) {
        return userService.queryById(userid);
    }

    // 新增用户
    @PostMapping
    public void insert(@RequestBody User user) {
        userService.insert(user);
    }

    // 更新用户信息
    @PutMapping("/{id}")
    public void update(@PathVariable("id") Integer userid, @RequestBody User user) {
        user.setUserid(userid); // 确保id一致
        userService.update(user);
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Integer userid) {
        userService.deleteById(userid);
    }

    // 用户登录
//    @PostMapping("/login")
//    @ResponseBody
//    public ResponseEntity<Object> login(String username, String password) {
//        System.out.println("user = " + user);
//        boolean success = userService.login(user.getUsername(), user.getPassword());
//        LoginInfo success = userService.login(username, password);
//        return ResponseEntity.ok(success);
//        if (success != null) {
//            return ResponseEntity.ok(success);
//        } else {
//            return ResponseEntity.badRequest().body("{\"success\": false, \"message\": \"登录失败\"}");
//        }
//    }
    @PostMapping("/login")
    public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) {
        String username = request.getUsername();
        String password = request.getPassword();

        // 从数据库中查询用户
        User user = userService.queryByUsername(username);

        if (user == null) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new LoginResponse(false, "用户名或密码错误"));
        }

        // 比对密码（假设密码是加密存储的）
        if (!password.equals(user.getPassword())) {
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new LoginResponse(false, "用户名或密码错误"));
        }

        // 登录成功，这里可以返回一些用户信息
        return ResponseEntity.ok(new LoginResponse(true, "登录成功"));
    }


    @PostMapping("/register")
    @ResponseBody
    public ResponseEntity<String> register(@RequestBody User user) {
        System.out.println("user = " + user);
        boolean success = userService.register(user.getUsername(), user.getPassword(), user.getEmail());
        if (success) {
            return ResponseEntity.ok("{\"success\": true, \"message\": \"注册成功\"}");
        } else {
            return ResponseEntity.badRequest().body("{\"success\": false, \"message\": \"注册失败\"}");
        }
    }

    @GetMapping("/hello")
    public String hello() {
        return "loginIn";
    }
}
